//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class SeriesText : IOfficeXmlSerializable, INotifyPropertyChanged //CT_SerTx
    {
        private StringReference _strRef;
        private string _value;

        public SeriesText()
        {
        }

        public SeriesText(string value)
        {
            _value = value;
        }

        public StringReference Reference { get { return StringReference; } set { this.StringReference = value; } }
        internal StringReference StringReference
        {
            get { return _strRef; }
            set
            {
                if (_strRef != null)
                    _strRef.PropertyChanged -= _strRef_PropertyChanged;
                _strRef = value;
                if (_strRef != null)
                    _strRef.PropertyChanged += _strRef_PropertyChanged;
                if (value != null)
                    _value = null;
            }
        }

        /*public string Value
        {
            get { return _value; }
            set { _value = value; }
        }*/

        public string Value {
            get { return _value; }
            set { _value = value; if (value != null)
            Reference = null;} 
        }
        ///<summary>
        /// Get the actual string value of the text.
        ///</summary>
        public string ActualValue
        {
            get
            {
                if (_value != null)
                    return _value;
                if (_strRef != null)
                    foreach (StringValue d in _strRef.StringValues)
                        return d.Value;
                return null;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("tx"))
            {
                c.PassAttributes();
                switch (c.ReaderLocalName)
                {
                    case "strRef":
                        StringReference = StringReference.ReadStringReference(c);
                        break;
                    case "v":
                        _value = c.ReadElementString("v");
                        break;
                    default:
                        throw new NotSupportedException();
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("tx"))
            {
                if (StringReference != null)
                    c.WriteElement(StringReference);
                if (_value != null)
                    c.WriteElementString("v", _value);
            }
        }

        #endregion

        private void _strRef_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            notifyPropertyChanged("ActualValue");
            notifyPropertyChanged("Value");
        }

        private void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}